/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kenneth
 */
public class UserMapper {
    
    private DatabaseConnection dbConn;
    
    public UserMapper(DatabaseConnection dbConn) {
          this.dbConn = dbConn;
    }
    
    public boolean authenticateUser(String email, String password) {
        boolean userExists = false; 
        dbConn.openConnection();
        try {   
            ResultSet res = dbConn.executeQuery("SELECT email FROM users WHERE email = '" + email + "' AND paswoord = '" + password + "';");
            if(res.next()) {
                userExists = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        dbConn.closeConnection();
        return userExists;
    }
    
    public int getIdByEmail(String email) {
        int id = 0;
        dbConn.openConnection();
        try {
            ResultSet res = dbConn.executeQuery("SELECT id FROM users WHERE email = '" + email + "';");
            if(res.next()) {
                id = res.getInt("id");
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(UserMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }
}
